package com.come4loves.game.dbproxy.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.come4loves.core.dbcenter.DBQueueType;
import com.come4loves.core.dbcenter.Entity;
import com.come4loves.core.dbcenter.annotation.Column;
import com.come4loves.core.dbcenter.annotation.Table;
import com.come4loves.core.utils.Splitable;
import com.come4loves.core.utils.StringUtils;
import com.come4loves.game.module.card.model.CardGroupModel;


@Table(name = "card_group", type = DBQueueType.DEFAULT)
public class CardGroup extends Entity<Long> {

	private static final long serialVersionUID = 2883368741230539057L;

	@Column(pk = true)
	public long actorId;
	/**
	 * 卡牌组
	 */
	@Column
	public String cardGroups;
	
	public Map<Integer, CardGroupModel> cardGroupMap = new HashMap<Integer, CardGroupModel>();

	@Override
	public Long getPkId() {
		return this.actorId;
	}

	@Override
	public void setPkId(Long pk) {
		this.actorId = pk;
	}

	@Override
	protected Entity<Long> readData(ResultSet rs, int rowNum) throws SQLException {
		CardGroup card = new CardGroup();
		card.actorId = rs.getLong("actorId");
		card.cardGroups = rs.getString("cardGroups");
		return card;
	}

	@Override
	protected void hasReadEvent() {
		List<CardGroupModel> delimiterString2List = StringUtils.delimiterString2List(cardGroups, CardGroupModel.class, Splitable.ELEMENT_SPLIT);
		for (CardGroupModel cardGroupModel : delimiterString2List) {
			this.cardGroupMap.put(cardGroupModel.groupId, cardGroupModel);
		}
	}
	
	@Override
	protected ArrayList<Object> writeData(Boolean containsPK) {
		ArrayList<Object> values = new ArrayList<Object>();
		if (containsPK) {
			values.add(this.actorId);
		}
		values.add(this.cardGroups);
		return values;
	}

	@Override
	protected void beforeWritingEvent() {
		this.cardGroups = StringUtils.collection2SplitString(this.cardGroupMap.values(), Splitable.ELEMENT_DELIMITER);
	}

	@Override
	protected void disposeBlob() {
		this.cardGroups = EMPTY_STRING;
	}
	
	public CardGroupModel getGroupModel(int groupId) {
		return this.cardGroupMap.get(groupId);
	}

	public boolean isContainsKeyModel(int groupId) {
		return this.cardGroupMap.containsKey(groupId);
	}
	
	public static CardGroupModel convert(long playerId, CardGroup cGroup, int groupId) {
		return cGroup.cardGroupMap.get(groupId);
	}
	
}
